home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / MoreFiles 1.2.1 / MoreFilesExtras.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  60.1 KB  |  1,992 lines  |  [TEXT/KAHL]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    A collection of useful high-level File Manager routines.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support
  7. **
  8. **    File:        MoreFilesExtras.c
  9. **
  10. **    Copyright © 1992-1994 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #ifndef __MOREFILESEXTRAS__
  23. #include "MoreFilesExtras.h"
  24. #endif
  25.  
  26. /*****************************************************************************/
  27.  
  28. /* local data structures */
  29.  
  30. /* The DeleteEnumGlobals structure is used to minimize the amount of
  31. ** stack space used when recursively calling DeleteLevel and to hold
  32. ** global information that might be needed at any time. */
  33.  
  34. #if defined(powerc) || defined (__powerc)
  35. #pragma options align=mac68k
  36. #endif
  37. struct DeleteEnumGlobals
  38. {
  39.     OSErr            error;                /* temporary holder of results - saves 2 bytes of stack each level */
  40.     Str63            itemName;            /* the name of the current item */
  41.     UniversalFMPB    myPB;                /* the parameter block used for PBGetCatInfo calls */
  42. };
  43. #if defined(powerc) || defined(__powerc)
  44. #pragma options align=reset
  45. #endif
  46.  
  47. typedef struct DeleteEnumGlobals DeleteEnumGlobals;
  48. typedef DeleteEnumGlobals *DeleteEnumGlobalsPtr;
  49.  
  50. /*****************************************************************************/
  51.  
  52. /* A utility routine called by NameFileSearch and CreatorTypeFileSearch to
  53. ** allocate a optimization buffer. */
  54.  
  55. static    Ptr    GetSearchBuffer(long *buffSize)
  56. {
  57.     Ptr    tempPtr;
  58.     
  59.     *buffSize = 0x00004000;        /* start by trying for a 16K buffer */
  60.     while ( *buffSize != 0 )    /* and keep trying until *buffSize is zero */
  61.     {
  62.         tempPtr = NewPtr(*buffSize);
  63.         if ( tempPtr != NULL )
  64.             break;    /* got it */
  65.         else
  66.             *buffSize -= 0x00000400;    /* didn't get it, subtract 1K and try again */
  67.     }
  68.     return ( tempPtr );
  69. }
  70.  
  71. /*****************************************************************************/
  72.  
  73. pascal    OSErr    NameFileSearch(StringPtr volName,
  74.                                short vRefNum,
  75.                                ConstStr255Param fileName,
  76.                                FSSpecPtr matches,
  77.                                long reqMatchCount,
  78.                                long *actMatchCount,
  79.                                Boolean newSearch,
  80.                                Boolean partial)
  81. {
  82.     CInfoPBRec        searchInfo1, searchInfo2;
  83.     HParamBlockRec    pb;
  84.     OSErr            error;
  85.     static CatPositionRec catPosition;
  86.     static short    lastVRefNum = 0;
  87.     
  88.     /* get the real volume reference number */
  89.     error = DetermineVRefNum(volName, vRefNum, &vRefNum);
  90.     if ( error != noErr )
  91.         return ( error );
  92.     
  93.     pb.csParam.ioNamePtr = NULL;
  94.     pb.csParam.ioVRefNum = vRefNum;
  95.     pb.csParam.ioMatchPtr = matches;
  96.     pb.csParam.ioReqMatchCount = reqMatchCount;
  97.     pb.csParam.ioSearchBits = ( partial ) ?        /* tell CatSearch what we're looking for: */
  98.         ( fsSBPartialName + fsSBFlAttrib ) :    /* partial name file matches or */
  99.         ( fsSBFullName + fsSBFlAttrib );        /* full name file matches */
  100.     pb.csParam.ioSearchInfo1 = &searchInfo1;
  101.     pb.csParam.ioSearchInfo2 = &searchInfo2;
  102.     pb.csParam.ioSearchTime = 0;
  103.     if ( (newSearch) ||                /* If caller specified new search */
  104.          (lastVRefNum != vRefNum) )    /* or if last search was to another volume, */
  105.     {
  106.         catPosition.initialize = 0;    /* then search from beginning of catalog */
  107.     }
  108.     pb.csParam.ioCatPosition = catPosition;
  109.     pb.csParam.ioOptBuffer = GetSearchBuffer(&pb.csParam.ioOptBufSize);
  110.  
  111.     /* search for fileName */
  112.     searchInfo1.hFileInfo.ioNamePtr = (StringPtr)fileName;
  113.     searchInfo2.hFileInfo.ioNamePtr = NULL;
  114.     
  115.     /* only match files (not directories) */
  116.     searchInfo1.hFileInfo.ioFlAttrib = 0x00;
  117.     searchInfo2.hFileInfo.ioFlAttrib = ioDirMask;
  118.  
  119.     error = PBCatSearchSyncCompat((CSParamPtr)&pb);
  120.     
  121.     if ( (error == noErr) ||                            /* If no errors or the end of catalog was */
  122.          (error == eofErr) )                            /* found, then the call was successful so */
  123.     {
  124.         *actMatchCount = pb.csParam.ioActMatchCount;    /* return the match count */
  125.     }
  126.     else
  127.     {
  128.         *actMatchCount = 0;                            /* else no matches found */
  129.     }
  130.     
  131.     if ( (error == noErr) ||                        /* If no errors */
  132.          (error == catChangedErr) )                    /* or there was a change in the catalog */
  133.     {
  134.         catPosition = pb.csParam.ioCatPosition;
  135.         lastVRefNum = vRefNum;
  136.             /* we can probably start the next search where we stopped this time */
  137.     }
  138.     else
  139.     {
  140.         catPosition.initialize = 0;
  141.             /* start the next search from beginning of catalog */
  142.     }
  143.     
  144.     if ( pb.csParam.ioOptBuffer != NULL )
  145.     {
  146.         DisposPtr(pb.csParam.ioOptBuffer);
  147.     }
  148.         
  149.     return ( error );
  150. }
  151.  
  152. /*****************************************************************************/
  153.  
  154. pascal    OSErr    CreatorTypeFileSearch(StringPtr volName,
  155.                                       short vRefNum,
  156.                                       OSType creator,
  157.                                       OSType fileType,
  158.                                       FSSpecPtr matches,
  159.                                       long reqMatchCount,
  160.                                       long *actMatchCount,
  161.                                       Boolean newSearch)
  162. {
  163.     CInfoPBRec        searchInfo1, searchInfo2;
  164.     HParamBlockRec    pb;
  165.     OSErr            error;
  166.     static CatPositionRec catPosition;
  167.     static short    lastVRefNum = 0;
  168.     
  169.     /* get the real volume reference number */
  170.     error = DetermineVRefNum(volName, vRefNum, &vRefNum);
  171.     if ( error != noErr )
  172.         return ( error );
  173.     
  174.     pb.csParam.ioNamePtr = NULL;
  175.     pb.csParam.ioVRefNum = vRefNum;
  176.     pb.csParam.ioMatchPtr = matches;
  177.     pb.csParam.ioReqMatchCount = reqMatchCount;
  178.     pb.csParam.ioSearchBits = fsSBFlAttrib + fsSBFlFndrInfo;    /* Looking for finder info file matches */
  179.     pb.csParam.ioSearchInfo1 = &searchInfo1;
  180.     pb.csParam.ioSearchInfo2 = &searchInfo2;
  181.     pb.csParam.ioSearchTime = 0;
  182.     if ( (newSearch) ||                /* If caller specified new search */
  183.          (lastVRefNum != vRefNum) )    /* or if last search was to another volume, */
  184.     {
  185.         catPosition.initialize = 0;    /* then search from beginning of catalog */
  186.     }
  187.     pb.csParam.ioCatPosition = catPosition;
  188.     pb.csParam.ioOptBuffer = GetSearchBuffer(&pb.csParam.ioOptBufSize);
  189.  
  190.     /* no fileName */
  191.     searchInfo1.hFileInfo.ioNamePtr = NULL;
  192.     searchInfo2.hFileInfo.ioNamePtr = NULL;
  193.     
  194.     /* only match files (not directories) */
  195.     searchInfo1.hFileInfo.ioFlAttrib = 0x00;
  196.     searchInfo2.hFileInfo.ioFlAttrib = ioDirMask;
  197.     
  198.     /* search for creator; if creator = 0x00000000, ignore creator */
  199.     searchInfo1.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  200.     searchInfo2.hFileInfo.ioFlFndrInfo.fdCreator = ( creator == (OSType)0x00000000 ) ?
  201.         (OSType)0x00000000 :
  202.         (OSType)0xffffffff;
  203.     
  204.     /* search for fileType; if fileType = 0x00000000, ignore fileType */
  205.     searchInfo1.hFileInfo.ioFlFndrInfo.fdType = fileType;
  206.     searchInfo2.hFileInfo.ioFlFndrInfo.fdType = ( fileType == (OSType)0x00000000 ) ?
  207.         (OSType)0x00000000 :
  208.         (OSType)0xffffffff;
  209.     
  210.     /* zero all other FInfo fields */
  211.     searchInfo1.hFileInfo.ioFlFndrInfo.fdFlags = 0;
  212.     searchInfo1.hFileInfo.ioFlFndrInfo.fdLocation.v = 0;
  213.     searchInfo1.hFileInfo.ioFlFndrInfo.fdLocation.h = 0;
  214.     searchInfo1.hFileInfo.ioFlFndrInfo.fdFldr = 0;
  215.     
  216.     searchInfo2.hFileInfo.ioFlFndrInfo.fdFlags = 0;
  217.     searchInfo2.hFileInfo.ioFlFndrInfo.fdLocation.v = 0;
  218.     searchInfo2.hFileInfo.ioFlFndrInfo.fdLocation.h = 0;
  219.     searchInfo2.hFileInfo.ioFlFndrInfo.fdFldr = 0;
  220.  
  221.     error = PBCatSearchSyncCompat((CSParamPtr)&pb);
  222.     
  223.     if ( (error == noErr) ||                            /* If no errors or the end of catalog was */
  224.          (error == eofErr) )                            /* found, then the call was successful so */
  225.     {
  226.         *actMatchCount = pb.csParam.ioActMatchCount;    /* return the match count */
  227.     }
  228.     else
  229.     {
  230.         *actMatchCount = 0;                            /* else no matches found */
  231.     }
  232.     
  233.     if ( (error == noErr) ||                        /* If no errors */
  234.          (error == catChangedErr) )                    /* or there was a change in the catalog */
  235.     {
  236.         catPosition = pb.csParam.ioCatPosition;
  237.         lastVRefNum = vRefNum;
  238.             /* we can probably start the next search where we stopped this time */
  239.     }
  240.     else
  241.     {
  242.         catPosition.initialize = 0;
  243.             /* start the next search from beginning of catalog */
  244.     }
  245.     
  246.     if ( pb.csParam.ioOptBuffer != NULL )
  247.     {
  248.         DisposPtr(pb.csParam.ioOptBuffer);
  249.     }
  250.         
  251.     return ( error );
  252. }
  253.  
  254. /*****************************************************************************/
  255.  
  256. pascal    OSErr    DetermineVRefNum(StringPtr pathname,
  257.                                  short vRefNum,
  258.                                  short *realVRefNum)
  259. {
  260.     HParamBlockRec pb;
  261.     Str255 tempPathname;
  262.     OSErr error;
  263.  
  264.     pb.volumeParam.ioVRefNum = vRefNum;
  265.     if ( pathname == NULL )
  266.     {
  267.         pb.volumeParam.ioNamePtr = NULL;
  268.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  269.     }
  270.     else
  271.     {
  272.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  273.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  274.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  275.     }
  276.     error = PBHGetVInfoSync(&pb);
  277.     *realVRefNum = pb.volumeParam.ioVRefNum;
  278.     return ( error );
  279. }
  280.  
  281. /*****************************************************************************/
  282.  
  283. pascal    OSErr    HGetVInfo(short volReference,
  284.                           StringPtr volName,
  285.                           short *vRefNum,
  286.                           unsigned long *freeBytes,
  287.                           unsigned long *totalBytes)
  288. {
  289.     HParamBlockRec    pb;
  290.     unsigned long    allocationBlockSize;
  291.     unsigned short    numAllocationBlocks;
  292.     unsigned short    numFreeBlocks;
  293.     VCB                *theVCB;
  294.     Boolean            vcbFound;
  295.     OSErr            result;
  296.     
  297.     /* Use the File Manager to get the real vRefNum */
  298.     pb.volumeParam.ioVRefNum = volReference;
  299.     pb.volumeParam.ioNamePtr = volName;
  300.     pb.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only, return volume name */
  301.     result = PBHGetVInfoSync(&pb);
  302.     
  303.     if ( result == noErr )
  304.     {
  305.         /* The volume name was returned in volName (if not NULL) and */
  306.         /* we have the volume's vRefNum and allocation block size */
  307.         *vRefNum = pb.volumeParam.ioVRefNum;
  308.         allocationBlockSize = (unsigned long)pb.volumeParam.ioVAlBlkSiz;
  309.         
  310.         /* System 7.5 (and beyond) pins the number of allocation blocks and */
  311.         /* the number of free allocation blocks returned by PBHGetVInfo to */
  312.         /* a value so that when multiplied by the allocation block size, */
  313.         /* the volume will look like it has $7fffffff bytes or less. This */
  314.         /* was done so older applications that use signed math or that use */
  315.         /* the GetVInfo function (which uses signed math) will continue to work. */
  316.         /* However, the unpinned numbers (which we want) are always available */
  317.         /* in the volume's VCB so we'll get those values from the VCB if possible. */
  318.         
  319.         /* Find the volume's VCB */
  320.         vcbFound = false;
  321.         theVCB = (VCB *)(GetVCBQHdr()->qHead);
  322.         while ( (theVCB != NULL) && !vcbFound )
  323.         {
  324.             /* Check VCB signature before using VCB. Don't have to check for */
  325.             /* MFS (0xd2d7) because they can't get big enough to be pinned */
  326.             if ( theVCB->vcbSigWord == 0x4244 )
  327.             {
  328.                 if ( theVCB->vcbVRefNum == *vRefNum )
  329.                 {
  330.                     vcbFound = true;
  331.                 }
  332.             }
  333.             
  334.             if ( !vcbFound )
  335.             {
  336.                 theVCB = (VCB *)(theVCB->qLink);
  337.             }
  338.         }
  339.         
  340.         if ( theVCB != NULL )
  341.         {
  342.             /* Found a VCB we can use. Get the un-pinned number of allocation blocks */
  343.             /* and the number of free blocks from the VCB. */
  344.             numAllocationBlocks = (unsigned short)theVCB->vcbNmAlBlks;
  345.             numFreeBlocks = (unsigned short)theVCB->vcbFreeBks;
  346.         }
  347.         else
  348.         {
  349.             /* Didn't find a VCB we can use. Return the number of allocation blocks */
  350.             /* and the number of free blocks returned by PBHGetVInfoSync. */
  351.             numAllocationBlocks = (unsigned short)pb.volumeParam.ioVNmAlBlks;
  352.             numFreeBlocks = (unsigned short)pb.volumeParam.ioVFrBlk;
  353.         }
  354.         
  355.         /* Now, calculate freeBytes and totalBytes using unsigned values */
  356.         *freeBytes = numFreeBlocks * allocationBlockSize;
  357.         *totalBytes = numAllocationBlocks * allocationBlockSize;
  358.     }
  359.     
  360.     return ( result );
  361. }
  362.  
  363. /*****************************************************************************/
  364.  
  365. pascal    OSErr    CheckVolLock(StringPtr pathname,
  366.                              short vRefNum)
  367. {
  368.     HParamBlockRec pb;
  369.     Str255 tempPathname;
  370.     OSErr error;
  371.  
  372.     pb.volumeParam.ioVRefNum = vRefNum;
  373.     if ( pathname == NULL )
  374.     {
  375.         pb.volumeParam.ioNamePtr = NULL;
  376.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  377.     }
  378.     else
  379.     {
  380.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  381.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  382.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  383.     }
  384.     error = PBHGetVInfoSync(&pb);
  385.     
  386.     if ( error == noErr )
  387.     {
  388.         if ( (pb.volumeParam.ioVAtrb & 0x0080) != 0 )
  389.             error = wPrErr;        /* volume locked by hardware */
  390.         else if ( (pb.volumeParam.ioVAtrb & 0x8000) != 0 )
  391.             error = vLckdErr;    /* volume locked by software */
  392.     }
  393.     
  394.     return ( error );
  395. }
  396.  
  397. /*****************************************************************************/
  398.  
  399. pascal    OSErr    UnmountAndEject(StringPtr pathname,
  400.                                 short vRefNum)
  401. {
  402.     HParamBlockRec pb;
  403.     Str255 tempPathname;
  404.     short driveNum;
  405.     Boolean ejected, wantsEject;
  406.     DrvQElPtr drvQElem;
  407.     OSErr error;
  408.  
  409.     pb.volumeParam.ioVRefNum = vRefNum;
  410.     if ( pathname == NULL )
  411.     {
  412.         pb.volumeParam.ioNamePtr = NULL;
  413.         pb.volumeParam.ioVolIndex = 0;        /* use ioVRefNum only */
  414.     }
  415.     else
  416.     {
  417.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  418.         pb.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  419.         pb.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  420.     }
  421.     error = PBHGetVInfoSync(&pb);
  422.     if ( error == noErr )
  423.     {
  424.         if ( pb.volumeParam.ioVDrvInfo != 0 )
  425.         {
  426.             /* the volume is online and not ejected */
  427.             ejected = false;
  428.             
  429.             /* Get the drive number */
  430.             driveNum = pb.volumeParam.ioVDrvInfo;
  431.         }
  432.         else
  433.         {
  434.             /* the volume is ejected or offline */
  435.             
  436.             /* Is it ejected? */
  437.             ejected = pb.volumeParam.ioVDRefNum > 0;
  438.             
  439.             if ( ejected )
  440.             {
  441.                 /* If ejected, the drive number is ioVDRefNum */
  442.                 driveNum = pb.volumeParam.ioVDRefNum;
  443.             }
  444.             else
  445.             {
  446.                 /* If offline, the drive number is the negative of ioVDRefNum */
  447.                 driveNum = (short)-pb.volumeParam.ioVDRefNum;
  448.             }
  449.         }
  450.         
  451.         /* find the drive queue element */
  452.         drvQElem = (DrvQElPtr)(GetDrvQHdr()->qHead);
  453.         while ( (drvQElem != NULL) && (drvQElem->dQDrive != driveNum) )
  454.         {
  455.             drvQElem = (DrvQElPtr)drvQElem->qLink;
  456.         }
  457.         
  458.         if ( drvQElem != NULL )
  459.         {
  460.             /* does the drive want an eject call */
  461.             wantsEject = (*((Ptr)((Ptr)drvQElem - 3)) != 8);
  462.         }
  463.         else
  464.         {
  465.             /* didn't find the drive!! */
  466.             wantsEject = false;
  467.         }
  468.         
  469.         /* unmount the volume */
  470.         pb.volumeParam.ioNamePtr = NULL;
  471.         /* ioVRefNum is already filled in from PBHGetVInfo */
  472.         error = PBUnmountVol((ParmBlkPtr)&pb);
  473.         if ( error == noErr )
  474.         {
  475.             if ( wantsEject && !ejected )
  476.             {
  477.                 /* eject the media from the drive if needed */
  478.                 pb.volumeParam.ioVRefNum = driveNum;
  479.                 error = PBEject((ParmBlkPtr)&pb);
  480.             }
  481.         }
  482.     }
  483.     return ( error );
  484. }
  485.  
  486. /*****************************************************************************/
  487.  
  488. pascal    OSErr    OnLine(FSSpecPtr volumes,
  489.                        short reqVolCount,
  490.                        short *actVolCount,
  491.                        short *volIndex)
  492. {
  493.     HParamBlockRec pb;
  494.     OSErr error = noErr;
  495.     FSSpec *endVolArray = volumes + reqVolCount;
  496.  
  497.     *actVolCount = 0;
  498.     for ( ; (volumes < endVolArray) && (error == noErr); ++volumes )
  499.     {
  500.         pb.volumeParam.ioNamePtr = (StringPtr) & volumes->name;
  501.         pb.volumeParam.ioVolIndex = *volIndex;
  502.         error = PBHGetVInfoSync(&pb);
  503.         if ( error == noErr )
  504.         {
  505.             volumes->parID = fsRtParID;        /* the root directory's parent is 1 */
  506.             volumes->vRefNum = pb.volumeParam.ioVRefNum;
  507.             ++*volIndex;
  508.             ++*actVolCount;
  509.         }
  510.     }
  511.     return ( error );
  512. }
  513.  
  514. /*****************************************************************************/
  515.  
  516. pascal    OSErr GetDInfo(short vRefNum,
  517.                        long dirID,
  518.                        StringPtr name,
  519.                        DInfo *fndrInfo)
  520. {
  521.     CInfoPBRec pb;
  522.     OSErr error;
  523.  
  524.     pb.dirInfo.ioNamePtr = name;
  525.     pb.dirInfo.ioVRefNum = vRefNum;
  526.     pb.dirInfo.ioDrDirID = dirID;
  527.     pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDrDirID */
  528.     error = PBGetCatInfoSync(&pb);
  529.     if ( error == noErr )
  530.     {
  531.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  532.             /* it's a directory, return the DInfo */
  533.             *fndrInfo = pb.dirInfo.ioDrUsrWds;
  534.         else
  535.             /* oops, a file was passed */
  536.             error = dirNFErr;
  537.     }
  538.     return ( error );
  539. }
  540.  
  541. /*****************************************************************************/
  542.  
  543. pascal    OSErr FSpGetDInfo(const FSSpec *spec,
  544.                           DInfo *fndrInfo)
  545. {
  546.     return ( GetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  547. }
  548.  
  549. /*****************************************************************************/
  550.  
  551. pascal    OSErr SetDInfo(short vRefNum,
  552.                        long dirID,
  553.                        StringPtr name,
  554.                        const DInfo *fndrInfo)
  555. {
  556.     CInfoPBRec pb;
  557.     OSErr error;
  558.  
  559.     pb.dirInfo.ioNamePtr = name;
  560.     pb.dirInfo.ioVRefNum = vRefNum;
  561.     pb.dirInfo.ioDrDirID = dirID;
  562.     pb.dirInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDrDirID */
  563.     error = PBGetCatInfoSync(&pb);
  564.     if ( error == noErr )
  565.     {
  566.         if ( (pb.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  567.         {
  568.             /* it's a directory, set the DInfo */
  569.             pb.dirInfo.ioDrUsrWds = *fndrInfo;
  570.             pb.dirInfo.ioDrDirID = dirID;
  571.             error = PBSetCatInfoSync(&pb);
  572.         }
  573.         else
  574.             /* oops, a file was passed */
  575.             error = dirNFErr;
  576.     }
  577.     return ( error );
  578. }
  579.  
  580. /*****************************************************************************/
  581.  
  582. pascal    OSErr FSpSetDInfo(const FSSpec *spec,
  583.                           const DInfo *fndrInfo)
  584. {
  585.     return ( SetDInfo(spec->vRefNum, spec->parID, (StringPtr)spec->name, fndrInfo) );
  586. }
  587.  
  588. /*****************************************************************************/
  589.  
  590. pascal    OSErr    GetDirID(short vRefNum,
  591.                          long dirID,
  592.                          StringPtr name,
  593.                          long *theDirID,
  594.                          Boolean *isDirectory)
  595. {
  596.     CInfoPBRec pb;
  597.     OSErr error;
  598.  
  599.     pb.hFileInfo.ioNamePtr = name;
  600.     pb.hFileInfo.ioVRefNum = vRefNum;
  601.     pb.hFileInfo.ioDirID = dirID;
  602.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  603.     error = PBGetCatInfoSync(&pb);
  604.     *theDirID = pb.hFileInfo.ioDirID;
  605.     *isDirectory = (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  606.     return ( error );
  607. }
  608.  
  609. /*****************************************************************************/
  610.  
  611. pascal    OSErr    DirIDFromFSSpec(const FSSpec *spec,
  612.                                 long *theDirID,
  613.                                 Boolean *isDirectory)
  614. {
  615.     return ( GetDirID(spec->vRefNum, spec->parID, (StringPtr)spec->name,
  616.              theDirID, isDirectory) );
  617. }
  618.  
  619. /*****************************************************************************/
  620.  
  621. pascal    OSErr    GetDirName(short vRefNum,
  622.                            long dirID,
  623.                            StringPtr name)
  624. {
  625.     CInfoPBRec pb;
  626.  
  627.     pb.hFileInfo.ioNamePtr = name;
  628.     pb.hFileInfo.ioVRefNum = vRefNum;
  629.     pb.hFileInfo.ioDirID = dirID;
  630.     pb.hFileInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  631.     return ( PBGetCatInfoSync(&pb) );
  632. }
  633.  
  634. /*****************************************************************************/
  635.  
  636. pascal    OSErr    GetParentID(short vRefNum,
  637.                             long dirID,
  638.                             StringPtr name,
  639.                             long *parID)
  640. {
  641.     CInfoPBRec pb;
  642.     OSErr error;
  643.     short realVRefNum;
  644.     
  645.     pb.hFileInfo.ioNamePtr = name;
  646.     pb.hFileInfo.ioVRefNum = vRefNum;
  647.     pb.hFileInfo.ioDirID = dirID;
  648.     pb.hFileInfo.ioFDirIndex = 0;                /* use ioNamePtr and ioDirID */
  649.     error = PBGetCatInfoSync(&pb);
  650.     if ( error == noErr )
  651.     {
  652.         /*
  653.         **    There's a bug in HFS where the wrong parent dir ID can be
  654.         **    returned if multiple separators are used at the end of a
  655.         **    pathname. For example, if the pathname:
  656.         **        'volumeName:System Folder:Extensions::'
  657.         **    is passed, the directory ID of the Extensions folder is
  658.         **    returned in the ioFlParID field instead of fsRtDirID. Since
  659.         **    multiple separators at the end of a pathname always specifies
  660.         **    a directory, we only need to work-around cases where the
  661.         **    object is a directory and there are multiple separators at
  662.         **    the end of the name parameter.
  663.         */
  664.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  665.         {
  666.             /* Its a directory *
  667.             
  668.             /* is there a pathname? */
  669.             if ( name != NULL )    
  670.             {
  671.                 /* could it contain multiple separators? */
  672.                 if ( name[0] >= 2 )
  673.                 {
  674.                     /* does it contain multiple separators at the end? */
  675.                     if ( (name[name[0]] == ':') && (name[name[0] - 1] == ':') )
  676.                     {
  677.                         /* OK, then do the extra stuff to get the correct parID */
  678.                         
  679.                         /* Get the real vRefNum (this should not fail) */
  680.                         error = DetermineVRefNum(name, vRefNum, &realVRefNum);
  681.                         if ( error == noErr )
  682.                         {
  683.                             pb.dirInfo.ioNamePtr = NULL;    /* we don't need the parent's name */
  684.                             pb.dirInfo.ioVRefNum = realVRefNum;
  685.                             /* pb.dirInfo.ioDrDirID already contains the */
  686.                             /* dirID of the directory object */
  687.                             pb.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  688.                             error = PBGetCatInfoSync(&pb);
  689.                             /* now, pb.dirInfo.ioDrParID contains the correct parID */
  690.                         }
  691.                     }
  692.                 }
  693.             }
  694.         }
  695.         
  696.         /* if no errors, then pb.hFileInfo.ioFlParID (pb.dirInfo.ioDrParID) */
  697.         /* contains the parent ID */
  698.         *parID = pb.hFileInfo.ioFlParID;
  699.     }
  700.     return ( error );
  701. }
  702.  
  703. /*****************************************************************************/
  704.  
  705. pascal    OSErr    GetFilenameFromPathname(ConstStr255Param pathname,
  706.                                         Str255 filename)
  707. {
  708.     short    index;
  709.     short    nameEnd;
  710.  
  711.     /* default to no filename */
  712.     filename[0] = 0;
  713.  
  714.     /* check for no pathname */
  715.     if ( pathname == NULL )
  716.         return ( notAFileErr );
  717.     
  718.     /* get string length */
  719.     index = pathname[0];
  720.     
  721.     /* check for empty string */
  722.     if ( index == 0 )
  723.         return ( notAFileErr );
  724.     
  725.     /* skip over last trailing colon (if any) */
  726.     if ( pathname[index] == ':' )
  727.         --index;
  728.  
  729.     /* save the end of the string */
  730.     nameEnd = index;
  731.  
  732.     /* if pathname ends with multiple colons, then this pathname refers */
  733.     /* to a directory, not a file */
  734.     if ( pathname[index] == ':' )
  735.         return ( notAFileErr );
  736.         
  737.     
  738.     /* parse backwards until we find a colon or hit the beginning of the pathname */
  739.     while ( (index != 0) && (pathname[index] != ':') )
  740.     {
  741.         --index;
  742.     }
  743.     
  744.     /* if we parsed to the beginning of the pathname and the pathname ended */
  745.     /* with a colon, then pathname is a full pathname to a volume, not a file */
  746.     if ( (index == 0) && (pathname[pathname[0]] == ':') )
  747.         return ( notAFileErr );
  748.     
  749.     /* get the filename and return noErr */
  750.     filename[0] = (char)(nameEnd - index);
  751.     BlockMoveData(&pathname[index+1], &filename[1], nameEnd - index);
  752.     return ( noErr );
  753. }
  754.  
  755. /*****************************************************************************/
  756.  
  757. pascal    OSErr    GetObjectLocation(short vRefNum,
  758.                                   long dirID,
  759.                                   StringPtr pathname,
  760.                                   short *realVRefNum,
  761.                                   long *realParID,
  762.                                   Str255 realName,
  763.                                   Boolean *isDirectory)
  764. {
  765.     OSErr error;
  766.     UniversalFMPB pb;
  767.     Str255 tempPathname;
  768.     
  769.     /* clear results */
  770.     *realVRefNum = 0;
  771.     *realParID = 0;
  772.     realName[0] = 0;
  773.     
  774.     /*
  775.     **    Get the real vRefNum
  776.     */
  777.     pb.hPB.volumeParam.ioVRefNum = vRefNum;
  778.     if ( pathname == NULL )
  779.     {
  780.         pb.hPB.volumeParam.ioNamePtr = NULL;
  781.         pb.hPB.volumeParam.ioVolIndex = 0;    /* use ioVRefNum only */
  782.     }
  783.     else
  784.     {
  785.         BlockMoveData(pathname, tempPathname, pathname[0] + 1);    /* make a copy of the string and */
  786.         pb.hPB.volumeParam.ioNamePtr = (StringPtr)tempPathname;    /* use the copy so original isn't trashed */
  787.         pb.hPB.volumeParam.ioVolIndex = -1;    /* use ioNamePtr/ioVRefNum combination */
  788.     }
  789.     error = PBHGetVInfoSync(&pb.hPB);
  790.  
  791.     if ( error == noErr )
  792.     {
  793.         *realVRefNum = pb.hPB.volumeParam.ioVRefNum;    /* we have the real vRefNum */
  794.         
  795.         /*
  796.         **    Determine if the object already exists and if so,
  797.         **    get the real parent directory ID if it's a file
  798.         */
  799.         pb.ciPB.hFileInfo.ioNamePtr = (StringPtr)pathname;
  800.         pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  801.         pb.ciPB.hFileInfo.ioDirID = dirID;
  802.         pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  803.         error = PBGetCatInfoSync(&pb.ciPB);
  804.         
  805.         if ( error == noErr )
  806.         {
  807.             /*
  808.             **    The file system object is present and we have the file's real parID
  809.             */
  810.             
  811.             /*    Is it a directory or a file? */
  812.             *isDirectory = (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0;
  813.             if ( *isDirectory )
  814.             {
  815.                 /*
  816.                 **    It's a directory, get its name and parent dirID, and then we're done
  817.                 */
  818.                 
  819.                 pb.ciPB.dirInfo.ioNamePtr = realName;
  820.                 pb.ciPB.dirInfo.ioVRefNum = *realVRefNum;
  821.                 /* pb.ciPB.dirInfo.ioDrDirID already contains the dirID of the directory object */
  822.                 pb.ciPB.dirInfo.ioFDirIndex = -1;    /* get information about ioDirID */
  823.                 error = PBGetCatInfoSync(&pb.ciPB);
  824.                 
  825.                 /* get the parent ID here, because the file system can return the */
  826.                 /* wrong parent ID from the last call. */
  827.                 *realParID = pb.ciPB.dirInfo.ioDrParID;
  828.             }
  829.             else
  830.             {
  831.                 /*
  832.                 **    It's a file - use the parent directory ID from the last call
  833.                 **    to GetCatInfoparse, get the file name, and then we're done
  834.                 */
  835.                 *realParID = pb.ciPB.hFileInfo.ioFlParID;    
  836.                 error = GetFilenameFromPathname(pathname, realName);
  837.             }
  838.         }
  839.         else if ( error == fnfErr )
  840.         {
  841.             /*
  842.             **    The file system object is not present - see if its parent is present
  843.             */
  844.             
  845.             /*
  846.             **    Parse to get the object name from end of pathname
  847.             */
  848.             error = GetFilenameFromPathname(pathname, realName);
  849.             
  850.             /* if we can't get the object name from the end, we can't continue */
  851.             if ( error == noErr )
  852.             {
  853.                 /*
  854.                 **    What we want now is the pathname minus the object name
  855.                 **    for example:
  856.                 **    if pathname is 'vol:dir:file' tempPathname becomes 'vol:dir:'
  857.                 **    if pathname is 'vol:dir:file:' tempPathname becomes 'vol:dir:'
  858.                 **    if pathname is ':dir:file' tempPathname becomes ':dir:'
  859.                 **    if pathname is ':dir:file:' tempPathname becomes ':dir:'
  860.                 **    if pathname is ':file' tempPathname becomes ':'
  861.                 **    if pathname is 'file or file:' tempPathname becomes ''
  862.                 */
  863.                 
  864.                 /* get a copy of the pathname */
  865.                 BlockMoveData(pathname, tempPathname, pathname[0] + 1);
  866.                 
  867.                 /* remove the object name */
  868.                 tempPathname[0] -= realName[0];
  869.                 /* and the trailing colon (if any) */
  870.                 if ( pathname[pathname[0]] == ':' )
  871.                     --tempPathname[0];
  872.                 
  873.                 /* OK, now get the parent's directory ID */
  874.                 pb.ciPB.hFileInfo.ioNamePtr = (StringPtr)tempPathname;
  875.                 pb.ciPB.hFileInfo.ioVRefNum = vRefNum;
  876.                 pb.ciPB.hFileInfo.ioDirID = dirID;
  877.                 pb.ciPB.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  878.                 error = PBGetCatInfoSync(&pb.ciPB);
  879.                 *realParID = pb.ciPB.dirInfo.ioDrDirID;
  880.  
  881.                 *isDirectory = false;    /* we don't know what the object is really going to be */
  882.             }
  883.             
  884.             if ( error != noErr )
  885.                 error = dirNFErr;    /* couldn't find parent directory */
  886.             else
  887.                 error = fnfErr;    /* we found the parent, but not the file */
  888.         }
  889.     }
  890.     return ( error );
  891. }
  892.  
  893. /*****************************************************************************/
  894.  
  895. pascal    OSErr    GetDirItems(short vRefNum,
  896.                             long dirID,
  897.                             StringPtr name,
  898.                             Boolean getFiles,
  899.                             Boolean getDirectories,
  900.                             FSSpecPtr items,
  901.                             short reqItemCount,
  902.                             short *actItemCount,
  903.                             short *itemIndex) /* start with 1, then use what's returned */
  904. {
  905.     CInfoPBRec pb;
  906.     OSErr error = noErr;
  907.     long theDirID;
  908.     Boolean isDirectory;
  909.     FSSpec *endItemsArray = items + reqItemCount;
  910.  
  911.     /* NOTE: If I could be sure that the caller passed a real vRefNum and real directory */
  912.     /* to this routine, I could rip out calls to DetermineVRefNum and GetDirID and this */
  913.     /* routine would be much faster because of the overhead of DetermineVRefNum and */
  914.     /* GetDirID and because GetDirID blows away the directory index hint the Macintosh */
  915.     /* file system keeps for indexed calls. I can't be sure, so for maximum throughput, */
  916.     /* pass a big array of FSSpecs so you can get the directory's contents with few calls */
  917.     /* to this routine. */
  918.     
  919.     /* get the real volume reference number */
  920.     error = DetermineVRefNum(name, vRefNum, &pb.hFileInfo.ioVRefNum);
  921.     if ( error != noErr )
  922.         return ( error );
  923.     
  924.     /* and the real directory ID of this directory (and make sure it IS a directory) */
  925.     error = GetDirID(vRefNum, dirID, name, &theDirID, &isDirectory);
  926.     if ( error != noErr )
  927.         return ( error );
  928.     else if ( !isDirectory )
  929.         return ( dirNFErr );
  930.  
  931.  
  932.     *actItemCount = 0;
  933.     for ( ; (items < endItemsArray) && (error == noErr); )
  934.     {
  935.         pb.hFileInfo.ioNamePtr = (StringPtr) &items->name;
  936.         pb.hFileInfo.ioDirID = theDirID;
  937.         pb.hFileInfo.ioFDirIndex = *itemIndex;
  938.         error = PBGetCatInfoSync(&pb);
  939.         if ( error == noErr )
  940.         {
  941.             items->parID = pb.hFileInfo.ioFlParID;    /* return item's parID */
  942.             items->vRefNum = pb.hFileInfo.ioVRefNum;    /* return item's vRefNum */
  943.             ++*itemIndex;    /* prepare to get next item in directory */
  944.             
  945.             if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )
  946.             {
  947.                 if ( getDirectories )
  948.                 {
  949.                     ++*actItemCount; /* keep this item */
  950.                     ++items; /* point to next item */
  951.                 }
  952.             }
  953.             else
  954.             {
  955.                 if ( getFiles )
  956.                 {
  957.                     ++*actItemCount; /* keep this item */
  958.                     ++items; /* point to next item */
  959.                 }
  960.             }
  961.         }
  962.     }
  963.     return ( error );
  964. }
  965.  
  966. /*****************************************************************************/
  967.  
  968. static    void    DeleteLevel(long dirToDelete,
  969.                             DeleteEnumGlobalsPtr theGlobals)
  970. {
  971.     long savedDir;
  972.     
  973.     do
  974.     {
  975.         /* prepare to delete directory */
  976.         theGlobals->myPB.ciPB.dirInfo.ioNamePtr = (StringPtr)&theGlobals->itemName;
  977.         theGlobals->myPB.ciPB.dirInfo.ioFDirIndex = 1;    /* get first item */
  978.         theGlobals->myPB.ciPB.dirInfo.ioDrDirID = dirToDelete;    /* in this directory */
  979.         theGlobals->error = PBGetCatInfoSync(&(theGlobals->myPB.ciPB));
  980.         if ( theGlobals->error == noErr )
  981.         {
  982.             savedDir = dirToDelete;
  983.             /* We have an item.  Is it a file or directory? */
  984.             if ( (theGlobals->myPB.ciPB.dirInfo.ioFlAttrib & ioDirMask) != 0 )
  985.             {
  986.                 /* it's a directory */
  987.                 savedDir = theGlobals->myPB.ciPB.dirInfo.ioDrDirID;    /* save dirID of directory instead */
  988.                 DeleteLevel(theGlobals->myPB.ciPB.dirInfo.ioDrDirID, theGlobals);    /* Delete its contents */
  989.                 theGlobals->myPB.ciPB.dirInfo.ioNamePtr = NULL;    /* prepare to delete directory */
  990.             }
  991.             if ( theGlobals->error == noErr )
  992.             {
  993.                 theGlobals->myPB.ciPB.dirInfo.ioDrDirID = savedDir;    /* restore dirID */
  994.                 theGlobals->myPB.hPB.fileParam.ioFVersNum = 0;    /* just in case it's used on an MFS volume... */
  995.                 theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* delete this item */
  996.                 if ( theGlobals->error == fLckdErr )
  997.                 {
  998.                     (void) PBHRstFLockSync(&(theGlobals->myPB.hPB));    /* unlock it */
  999.                     theGlobals->error = PBHDeleteSync(&(theGlobals->myPB.hPB));    /* and try again */
  1000.                 }
  1001.             }
  1002.         }
  1003.     } while ( theGlobals->error == noErr );
  1004.     
  1005.     if ( theGlobals->error == fnfErr )
  1006.         theGlobals->error = noErr;
  1007. }
  1008.  
  1009. /*****************************************************************************/
  1010.  
  1011. pascal    OSErr    DeleteDirectoryContents(short vRefNum,
  1012.                                          long dirID,
  1013.                                         StringPtr name)
  1014. {
  1015.     DeleteEnumGlobals theGlobals;
  1016.     Boolean    isDirectory;
  1017.     OSErr error;
  1018.  
  1019.     /*  Get the real dirID and make sure it is a directory. */
  1020.     error = GetDirID(vRefNum, dirID, name, &dirID, &isDirectory);
  1021.     if ( error != noErr )
  1022.         return ( error );
  1023.     if ( !isDirectory )
  1024.         return ( dirNFErr );
  1025.     
  1026.     /* Get the real vRefNum */
  1027.     error = DetermineVRefNum(name, vRefNum, &vRefNum);
  1028.     if ( error != noErr )
  1029.         return ( error );
  1030.     
  1031.     /* Set up the globals we need to access from the recursive routine. */
  1032.     theGlobals.myPB.ciPB.dirInfo.ioVRefNum = vRefNum;
  1033.         
  1034.     /* Here we go into recursion land... */
  1035.     DeleteLevel(dirID, &theGlobals);
  1036.     return ( theGlobals.error );
  1037. }
  1038.  
  1039. /*****************************************************************************/
  1040.  
  1041. pascal    OSErr    DeleteDirectory(short vRefNum,
  1042.                                 long dirID,
  1043.                                 StringPtr name)
  1044. {
  1045.     OSErr error;
  1046.     
  1047.     /* Make sure a directory was specified and then delete its contents */
  1048.     error = DeleteDirectoryContents(vRefNum, dirID, name);
  1049.     if ( error == noErr )
  1050.     {
  1051.         error = HDelete(vRefNum, dirID, name);
  1052.         if ( error == fLckdErr )
  1053.         {
  1054.             (void) HRstFLock(vRefNum, dirID, name);    /* unlock the directory locked by AppleShare */
  1055.             error = HDelete(vRefNum, dirID, name);    /* and try again */
  1056.         }
  1057.     }
  1058.     return ( error );
  1059. }
  1060.  
  1061. /*****************************************************************************/
  1062.  
  1063. pascal    OSErr    CheckObjectLock(short vRefNum,
  1064.                                 long dirID,
  1065.                                 StringPtr name)
  1066. {
  1067.     CInfoPBRec pb;
  1068.     OSErr error;
  1069.     
  1070.     pb.hFileInfo.ioNamePtr = name;
  1071.     pb.hFileInfo.ioVRefNum = vRefNum;
  1072.     pb.hFileInfo.ioDirID = dirID;
  1073.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1074.     error = PBGetCatInfoSync(&pb);
  1075.     
  1076.     if ( error == noErr )
  1077.     {
  1078.         /* check locked bit */
  1079.         if ( (pb.hFileInfo.ioFlAttrib & 0x01) != 0 )
  1080.             error = fLckdErr;
  1081.     }
  1082.     return ( error );
  1083. }
  1084.  
  1085. /*****************************************************************************/
  1086.  
  1087. pascal    OSErr    FSpCheckObjectLock(const FSSpec *spec)
  1088. {
  1089.     return ( CheckObjectLock(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1090. }
  1091.  
  1092. /*****************************************************************************/
  1093.  
  1094. pascal    OSErr    BumpDate(short vRefNum,
  1095.                          long dirID,
  1096.                          StringPtr name)
  1097. /* Given a file or directory, change its modification date to the current date/time. */
  1098. {
  1099.     CInfoPBRec pb;
  1100.     OSErr error;
  1101.     unsigned long secs;
  1102.  
  1103.     pb.hFileInfo.ioNamePtr = name;
  1104.     pb.hFileInfo.ioVRefNum = vRefNum;
  1105.     pb.hFileInfo.ioDirID = dirID;
  1106.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1107.     error = PBGetCatInfoSync(&pb);
  1108.     if ( error == noErr )
  1109.     {
  1110.         GetDateTime(&secs);
  1111.         /* set mod date to current date, or one second into the future
  1112.             if mod date = current date */
  1113.         pb.hFileInfo.ioFlMdDat = (secs == pb.hFileInfo.ioFlMdDat) ? (++secs) : (secs);
  1114.         pb.hFileInfo.ioDirID = dirID;
  1115.         error = PBSetCatInfoSync(&pb);
  1116.     }
  1117.     return ( error );
  1118. }
  1119.  
  1120. /*****************************************************************************/
  1121.  
  1122. pascal    OSErr    FSpBumpDate(const FSSpec *spec)
  1123. {
  1124.     return ( BumpDate(spec->vRefNum, spec->parID, (StringPtr)spec->name) );
  1125. }
  1126.  
  1127. /*****************************************************************************/
  1128.  
  1129. pascal    OSErr    ChangeCreatorType(short vRefNum,
  1130.                                   long dirID,
  1131.                                   ConstStr255Param name,
  1132.                                   OSType creator,
  1133.                                   OSType fileType)
  1134. {
  1135.     CInfoPBRec pb;
  1136.     OSErr error;
  1137.     short realVRefNum;
  1138.     long parID;
  1139.  
  1140.     pb.hFileInfo.ioNamePtr = (StringPtr)name;
  1141.     pb.hFileInfo.ioVRefNum = vRefNum;
  1142.     pb.hFileInfo.ioDirID = dirID;
  1143.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1144.     error = PBGetCatInfoSync(&pb);
  1145.     if ( error == noErr )
  1146.     {
  1147.         if ( (pb.hFileInfo.ioFlAttrib & ioDirMask) != 0 )    /* if directory */
  1148.             return ( notAFileErr );            /* do nothing and return error */
  1149.             
  1150.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1151.  
  1152.         /* If creator not 0x00000000, change creator */
  1153.         if ( creator != (OSType)0x00000000 )
  1154.             pb.hFileInfo.ioFlFndrInfo.fdCreator = creator;
  1155.         
  1156.         /* If fileType not 0x00000000, change fileType */
  1157.         if ( fileType != (OSType)0x00000000 )
  1158.             pb.hFileInfo.ioFlFndrInfo.fdType = fileType;
  1159.             
  1160.         pb.hFileInfo.ioDirID = dirID;
  1161.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1162.  
  1163.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1164.         {
  1165.             /* get the real vRefNum in case a full pathname was passed */
  1166.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1167.             if ( error == noErr )
  1168.             {
  1169.                 error = BumpDate(realVRefNum, parID, NULL);
  1170.                     /* and bump the parent directory's mod date to wake up the Finder */
  1171.                     /* to the change we just made */
  1172.             }
  1173.         }
  1174.     }
  1175.     return ( error );
  1176. }
  1177.  
  1178. /*****************************************************************************/
  1179.  
  1180. pascal    OSErr    FSpChangeCreatorType(const FSSpec *spec,
  1181.                                      OSType creator,
  1182.                                      OSType fileType)
  1183. {
  1184.     return ( ChangeCreatorType(spec->vRefNum, spec->parID, spec->name, creator, fileType) );
  1185. }
  1186.  
  1187. /*****************************************************************************/
  1188.  
  1189. pascal    OSErr    ChangeFDFlags(short vRefNum,
  1190.                               long dirID,
  1191.                               StringPtr name,
  1192.                               Boolean    setBits,
  1193.                               unsigned short flagBits)
  1194. {
  1195.     CInfoPBRec pb;
  1196.     OSErr error;
  1197.     short realVRefNum;
  1198.     long parID;
  1199.  
  1200.     pb.hFileInfo.ioNamePtr = name;
  1201.     pb.hFileInfo.ioVRefNum = vRefNum;
  1202.     pb.hFileInfo.ioDirID = dirID;
  1203.     pb.hFileInfo.ioFDirIndex = 0;    /* use ioNamePtr and ioDirID */
  1204.     error = PBGetCatInfoSync(&pb);
  1205.     if ( error == noErr )
  1206.     {
  1207.         parID = pb.hFileInfo.ioFlParID;    /* save parent dirID for BumpDate call */
  1208.  
  1209.         pb.hFileInfo.ioFlFndrInfo.fdFlags = ( setBits ) ? 
  1210.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags | flagBits ) : /* OR in the bits */
  1211.             ( pb.hFileInfo.ioFlFndrInfo.fdFlags & (0xffff ^ flagBits) ); /* AND out the bits */
  1212.                 /* set or clear the appropriate bits in the Finder flags */
  1213.             
  1214.         pb.hFileInfo.ioDirID = dirID;
  1215.         error = PBSetCatInfoSync(&pb);    /* now, save the new information back to disk */
  1216.  
  1217.         if ( (error == noErr) && (parID != fsRtParID) ) /* can't bump fsRtParID */
  1218.         {
  1219.             /* get the real vRefNum in case a full pathname was passed */
  1220.             error = DetermineVRefNum((StringPtr)name, vRefNum, &realVRefNum);
  1221.             if ( error == noErr )
  1222.             {
  1223.                 error = BumpDate(realVRefNum, parID, NULL);
  1224.                     /* and bump the parent directory's mod date to wake up the Finder */
  1225.                     /* to the change we just made */
  1226.             }
  1227.         }
  1228.     }
  1229.     return ( error );
  1230. }
  1231.  
  1232. /*****************************************************************************/
  1233.  
  1234. pascal    OSErr    FSpChangeFDFlags(const FSSpec *spec,
  1235.                                  Boolean setBits,
  1236.                                  unsigned short flagBits)
  1237. {
  1238.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, setBits, flagBits) );
  1239. }
  1240.  
  1241. /*****************************************************************************/
  1242.  
  1243. pascal    OSErr    SetIsInvisible(short vRefNum,
  1244.                                long dirID,
  1245.                                StringPtr name)
  1246.     /* Given a file or directory, make it invisible. */
  1247. {
  1248.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x4000) );
  1249. }
  1250.  
  1251. /*****************************************************************************/
  1252.  
  1253. pascal    OSErr    FSpSetIsInvisible(const FSSpec *spec)
  1254.     /* Given a file or directory, make it invisible. */
  1255. {
  1256.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x4000) );
  1257. }
  1258.  
  1259. /*****************************************************************************/
  1260.  
  1261. pascal    OSErr    ClearIsInvisible(short vRefNum,
  1262.                                  long dirID,
  1263.                                  StringPtr name)
  1264.     /* Given a file or directory, make it visible. */
  1265. {
  1266.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x4000) );
  1267. }
  1268.  
  1269. /*****************************************************************************/
  1270.  
  1271. pascal    OSErr    FSpClearIsInvisible(const FSSpec *spec)
  1272.     /* Given a file or directory, make it visible. */
  1273. {
  1274.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x4000) );
  1275. }
  1276.  
  1277. /*****************************************************************************/
  1278.  
  1279. pascal    OSErr    SetNameLocked(short vRefNum,
  1280.                               long dirID,
  1281.                               StringPtr name)
  1282.     /* Given a file or directory, lock its name. */
  1283. {
  1284.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x1000) );
  1285. }
  1286.  
  1287. /*****************************************************************************/
  1288.  
  1289. pascal    OSErr    FSpSetNameLocked(const FSSpec *spec)
  1290.     /* Given a file or directory, lock its name. */
  1291. {
  1292.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x1000) );
  1293. }
  1294.  
  1295. /*****************************************************************************/
  1296.  
  1297. pascal    OSErr    ClearNameLocked(short vRefNum,
  1298.                                 long dirID,
  1299.                                 StringPtr name)
  1300.     /* Given a file or directory, unlock its name. */
  1301. {
  1302.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x1000) );
  1303. }
  1304.  
  1305. /*****************************************************************************/
  1306.  
  1307. pascal    OSErr    FSpClearNameLocked(const FSSpec *spec)
  1308.     /* Given a file or directory, unlock its name. */
  1309. {
  1310.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x1000) );
  1311. }
  1312.  
  1313. /*****************************************************************************/
  1314.  
  1315. pascal    OSErr    SetIsStationery(short vRefNum,
  1316.                                 long dirID,
  1317.                                 ConstStr255Param name)
  1318.     /* Given a file, make it a stationery pad. */
  1319. {
  1320.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, true, 0x0800) );
  1321. }
  1322.  
  1323. /*****************************************************************************/
  1324.  
  1325. pascal    OSErr    FSpSetIsStationery(const FSSpec *spec)
  1326.     /* Given a file, make it a stationery pad. */
  1327. {
  1328.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0800) );
  1329. }
  1330.  
  1331. /*****************************************************************************/
  1332.  
  1333. pascal    OSErr    ClearIsStationery(short vRefNum,
  1334.                                   long dirID,
  1335.                                   ConstStr255Param name)
  1336.     /* Given a file, clear the stationery bit. */
  1337. {
  1338.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0800) );
  1339. }
  1340.  
  1341. /*****************************************************************************/
  1342.  
  1343. pascal    OSErr    FSpClearIsStationery(const FSSpec *spec)
  1344.     /* Given a file, clear the stationery bit. */
  1345. {
  1346.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0800) );
  1347. }
  1348.  
  1349. /*****************************************************************************/
  1350.  
  1351. pascal    OSErr    SetHasCustomIcon(short vRefNum,
  1352.                                  long dirID,
  1353.                                  StringPtr name)
  1354.     /* Given a file or directory, indicate that it has a custom icon. */
  1355. {
  1356.     return ( ChangeFDFlags(vRefNum, dirID, name, true, 0x0400) );
  1357. }
  1358.  
  1359. /*****************************************************************************/
  1360.  
  1361. pascal    OSErr    FSpSetHasCustomIcon(const FSSpec *spec)
  1362.     /* Given a file or directory, indicate that it has a custom icon. */
  1363. {
  1364.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, true, 0x0400) );
  1365. }
  1366.  
  1367. /*****************************************************************************/
  1368.  
  1369. pascal    OSErr    ClearHasCustomIcon(short vRefNum,
  1370.                                    long dirID,
  1371.                                    StringPtr name)
  1372.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1373. {
  1374.     return ( ChangeFDFlags(vRefNum, dirID, name, false, 0x0400) );
  1375. }
  1376.  
  1377. /*****************************************************************************/
  1378.  
  1379. pascal    OSErr    FSpClearHasCustomIcon(const FSSpec *spec)
  1380.     /* Given a file or directory, indicate that it does not have a custom icon. */
  1381. {
  1382.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0400) );
  1383. }
  1384.  
  1385. /*****************************************************************************/
  1386.  
  1387. pascal    OSErr    ClearHasBeenInited(short vRefNum,
  1388.                                    long dirID,
  1389.                                    StringPtr name)
  1390.     /* Given a file, clear its "has been inited" bit. */
  1391. {
  1392.     return ( ChangeFDFlags(vRefNum, dirID, (StringPtr)name, false, 0x0100) );
  1393. }
  1394.  
  1395. /*****************************************************************************/
  1396.  
  1397. pascal    OSErr    FSpClearHasBeenInited(const FSSpec *spec)
  1398.     /* Given a file, clear its "has been inited" bit. */
  1399. {
  1400.     return ( ChangeFDFlags(spec->vRefNum, spec->parID, (StringPtr)spec->name, false, 0x0100) );
  1401. }
  1402.  
  1403. /*****************************************************************************/
  1404.  
  1405. pascal    OSErr    CopyFileMgrAttributes(short srcVRefNum,
  1406.                                       long srcDirID,
  1407.                                       StringPtr srcName,
  1408.                                       short dstVRefNum,
  1409.                                       long dstDirID,
  1410.                                       StringPtr dstName,
  1411.                                       Boolean copyLockBit)
  1412. {
  1413.     UniversalFMPB pb;
  1414.     OSErr error;
  1415.     Boolean objectIsDirectory;
  1416.  
  1417.     pb.ciPB.hFileInfo.ioVRefNum = srcVRefNum;
  1418.     pb.ciPB.hFileInfo.ioDirID = srcDirID;
  1419.     pb.ciPB.hFileInfo.ioNamePtr = srcName;
  1420.     pb.ciPB.hFileInfo.ioFDirIndex = 0;
  1421.     error = PBGetCatInfoSync(&pb.ciPB);
  1422.     if ( error == noErr )
  1423.     {
  1424.         objectIsDirectory = ( (pb.ciPB.hFileInfo.ioFlAttrib & ioDirMask) != 0 );
  1425.         pb.ciPB.hFileInfo.ioVRefNum = dstVRefNum;
  1426.         pb.ciPB.hFileInfo.ioDirID = dstDirID;
  1427.         pb.ciPB.hFileInfo.ioNamePtr = dstName;
  1428.         /* don't copy the hasBeenInited bit */
  1429.         pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags = ( pb.ciPB.hFileInfo.ioFlFndrInfo.fdFlags & 0xfeff );
  1430.         error = PBSetCatInfoSync(&pb.ciPB);
  1431.         if ( (error == noErr) && (copyLockBit) && ((pb.ciPB.hFileInfo.ioFlAttrib & 0x01) != 0) )
  1432.         {
  1433.             pb.hPB.fileParam.ioFVersNum = 0;
  1434.             error = PBHSetFLockSync(&pb.hPB);
  1435.             if ( (error != noErr) && (objectIsDirectory) )
  1436.                 error = noErr; /* ignore lock errors if destination is directory */
  1437.         }
  1438.     }
  1439.     return ( error );
  1440. }
  1441.  
  1442. /*****************************************************************************/
  1443.  
  1444. pascal    OSErr    FSpCopyFileMgrAttributes(const FSSpec *srcSpec,
  1445.                                          const FSSpec *dstSpec,
  1446.                                          Boolean copyLockBit)
  1447. {
  1448.     return ( CopyFileMgrAttributes(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  1449.                                    dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name,
  1450.                                    copyLockBit) );
  1451. }
  1452.  
  1453. /*****************************************************************************/
  1454.  
  1455. pascal    OSErr    HOpenAware(short vRefNum,
  1456.                            long dirID,
  1457.                            ConstStr255Param fileName,
  1458.                            short denyModes,
  1459.                            short *refNum)
  1460. {
  1461.     HParamBlockRec pb;
  1462.     OSErr error;
  1463.     GetVolParmsInfoBuffer volParmsInfo;
  1464.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1465.  
  1466.     pb.ioParam.ioMisc = NULL;
  1467.     pb.fileParam.ioFVersNum = 0;
  1468.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1469.     pb.fileParam.ioVRefNum = vRefNum;
  1470.     pb.fileParam.ioDirID = dirID;
  1471.  
  1472.     /* get volume attributes */
  1473.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1474.     /* return the correct error result to the OpenDeny call */
  1475.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1476.     if ( error == noErr )
  1477.     {
  1478.         /* if volume supports OpenDeny, use it and return */
  1479.         if ( hasOpenDeny(volParmsInfo) )
  1480.         {
  1481.             pb.accessParam.ioDenyModes = denyModes;
  1482.             error = PBHOpenDenySync(&pb);
  1483.             *refNum = pb.ioParam.ioRefNum;
  1484.             return ( error );
  1485.         }
  1486.     }
  1487.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1488.         return ( error );
  1489.     
  1490.     /* OpenDeny isn't supported, so try File Manager Open functions */
  1491.     
  1492.     /* If request includes write permission, then see if the volume is */
  1493.     /* locked by hardware or software. The HFS file system doesn't check */
  1494.     /* for this when a file is opened - you only find out later when you */
  1495.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1496.     
  1497.     if ( (denyModes & dmWr) != 0 )
  1498.     {
  1499.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1500.         if ( error != noErr )
  1501.             return ( error );
  1502.     }
  1503.     
  1504.     /* Set File Manager permissions to closest thing possible */
  1505.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1506.                            (fsRdWrShPerm) :
  1507.                            (denyModes % 4);
  1508.  
  1509.     error = PBHOpenDFSync(&pb);                /* Try OpenDF */
  1510.     if ( error == paramErr )
  1511.         error = PBHOpenSync(&pb);            /* OpenDF not supported, so try Open */
  1512.     *refNum = pb.ioParam.ioRefNum;
  1513.     return ( error );
  1514. }
  1515.  
  1516. /*****************************************************************************/
  1517.  
  1518. pascal    OSErr    FSpOpenAware(const FSSpec *spec,
  1519.                              short denyModes,
  1520.                              short *refNum)
  1521. {
  1522.     return ( HOpenAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1523. }
  1524.  
  1525. /*****************************************************************************/
  1526.  
  1527. pascal    OSErr    HOpenRFAware(short vRefNum,
  1528.                              long dirID,
  1529.                              ConstStr255Param fileName,
  1530.                              short denyModes,
  1531.                              short *refNum)
  1532. {
  1533.     HParamBlockRec pb;
  1534.     OSErr error;
  1535.     GetVolParmsInfoBuffer volParmsInfo;
  1536.     long infoSize = sizeof(GetVolParmsInfoBuffer);
  1537.  
  1538.     pb.ioParam.ioMisc = NULL;
  1539.     pb.fileParam.ioFVersNum = 0;
  1540.     pb.fileParam.ioNamePtr = (StringPtr)fileName;
  1541.     pb.fileParam.ioVRefNum = vRefNum;
  1542.     pb.fileParam.ioDirID = dirID;
  1543.  
  1544.     /* get volume attributes */
  1545.     /* this preflighting is needed because Foreign File Access based file systems don't */
  1546.     /* return the correct error result to the OpenRFDeny call */
  1547.     error = HGetVolParms((StringPtr)fileName, vRefNum, &volParmsInfo, &infoSize);
  1548.     if ( error == noErr )
  1549.     {
  1550.         /* if volume supports OpenRFDeny, use it and return */
  1551.         if ( hasOpenDeny(volParmsInfo) )
  1552.         {
  1553.             pb.accessParam.ioDenyModes = denyModes;
  1554.             error = PBHOpenRFDenySync(&pb);
  1555.             *refNum = pb.ioParam.ioRefNum;
  1556.             return ( error );
  1557.         }
  1558.     }
  1559.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1560.         return ( error );
  1561.  
  1562.     /* OpenRFDeny isn't supported, so try File Manager OpenRF function */
  1563.     
  1564.     /* If request includes write permission, then see if the volume is */
  1565.     /* locked by hardware or software. The HFS file system doesn't check */
  1566.     /* for this when a file is opened - you only find out later when you */
  1567.     /* try to write and the write fails with a wPrErr or a vLckdErr. */
  1568.     
  1569.     if ( (denyModes & dmWr) != 0 )
  1570.     {
  1571.         error = CheckVolLock((StringPtr)fileName, vRefNum);
  1572.         if ( error != noErr )
  1573.             return ( error );
  1574.     }
  1575.     
  1576.     /* Set File Manager permissions to closest thing possible */
  1577.     pb.ioParam.ioPermssn = ((denyModes == dmWr) || (denyModes == dmRdWr)) ?
  1578.                            (fsRdWrShPerm) :
  1579.                            (denyModes % 4);
  1580.  
  1581.     error = PBHOpenRFSync(&pb);
  1582.     *refNum = pb.ioParam.ioRefNum;
  1583.     return ( error );
  1584. }
  1585.  
  1586. /*****************************************************************************/
  1587.  
  1588. pascal    OSErr    FSpOpenRFAware(const FSSpec *spec,
  1589.                                short denyModes,
  1590.                                short *refNum)
  1591. {
  1592.     return ( HOpenRFAware(spec->vRefNum, spec->parID, spec->name, denyModes, refNum) );
  1593. }
  1594.  
  1595. /*****************************************************************************/
  1596.  
  1597. pascal    OSErr    FSReadNoCache(short refNum,
  1598.                               long *count,
  1599.                               void *buffPtr)
  1600. {
  1601.     ParamBlockRec pb;
  1602.     OSErr error;
  1603.  
  1604.     pb.ioParam.ioRefNum = refNum;
  1605.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1606.     pb.ioParam.ioReqCount = *count;
  1607.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1608.     pb.ioParam.ioPosOffset = 0;
  1609.     error = PBReadSync(&pb);
  1610.     *count = pb.ioParam.ioActCount;
  1611.     return ( error );
  1612. }
  1613.  
  1614. /*****************************************************************************/
  1615.  
  1616. pascal    OSErr    FSWriteNoCache(short refNum,
  1617.                                long *count,
  1618.                                const void *buffPtr)
  1619. {
  1620.     ParamBlockRec pb;
  1621.     OSErr error;
  1622.  
  1623.     pb.ioParam.ioRefNum = refNum;
  1624.     pb.ioParam.ioBuffer = (Ptr)buffPtr;
  1625.     pb.ioParam.ioReqCount = *count;
  1626.     pb.ioParam.ioPosMode = fsAtMark + 0x0020;    /* fsAtMark + noCacheBit */
  1627.     pb.ioParam.ioPosOffset = 0;
  1628.     error = PBWriteSync(&pb);
  1629.     *count = pb.ioParam.ioActCount;
  1630.     return ( error );
  1631. }
  1632.  
  1633. /*****************************************************************************/
  1634.  
  1635. pascal    OSErr    CopyFork(short srcRefNum,
  1636.                          short dstRefNum,
  1637.                          void *copyBufferPtr,
  1638.                          long copyBufferSize)
  1639. {
  1640.     ParamBlockRec srcPB;
  1641.     ParamBlockRec dstPB;
  1642.     OSErr srcError;
  1643.     OSErr dstError;
  1644.  
  1645.     if ( (copyBufferPtr == NULL) || (copyBufferSize == 0) )
  1646.         return ( paramErr );
  1647.     
  1648.     srcPB.ioParam.ioRefNum = srcRefNum;
  1649.     dstPB.ioParam.ioRefNum = dstRefNum;
  1650.  
  1651.     /* preallocate the destination fork and */
  1652.     /* ensure the destination fork's EOF is correct after the copy */
  1653.     srcError = PBGetEOFSync(&srcPB);
  1654.     if ( srcError != noErr )
  1655.         return ( srcError );
  1656.     dstPB.ioParam.ioMisc = srcPB.ioParam.ioMisc;
  1657.     dstError = PBSetEOFSync(&dstPB);
  1658.     if ( dstError != noErr )
  1659.         return ( dstError );
  1660.  
  1661.     /* reset source fork's mark */
  1662.     srcPB.ioParam.ioPosMode = fsFromStart;
  1663.     srcPB.ioParam.ioPosOffset = 0;
  1664.     srcError = PBSetFPosSync(&srcPB);
  1665.     if ( srcError != noErr )
  1666.         return ( srcError );
  1667.  
  1668.     /* reset destination fork's mark */
  1669.     dstPB.ioParam.ioPosMode = fsFromStart;
  1670.     dstPB.ioParam.ioPosOffset = 0;
  1671.     dstError = PBSetFPosSync(&srcPB);
  1672.     if ( dstError != noErr )
  1673.         return ( dstError );
  1674.  
  1675.     /* set up fields that won't change in the loop */
  1676.     srcPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  1677.     srcPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  1678.     srcPB.ioParam.ioReqCount = ((copyBufferSize >= 512) && (copyBufferSize % 512)) ?
  1679.                                (copyBufferSize / 512) * 512 :
  1680.                                (copyBufferSize);
  1681.         /* If copyBufferSize is greater than 512 bytes, make it a multiple of 512 bytes */
  1682.         /* This will make writes on local volumes faster */
  1683.  
  1684.     dstPB.ioParam.ioBuffer = (Ptr)copyBufferPtr;
  1685.     dstPB.ioParam.ioPosMode = fsAtMark + 0x0020;/* fsAtMark + noCacheBit */
  1686.  
  1687.     while ( (srcError == noErr) && (dstError == noErr) )
  1688.     {
  1689.         srcError = PBReadSync(&srcPB);
  1690.         dstPB.ioParam.ioReqCount = srcPB.ioParam.ioActCount;
  1691.         dstError = PBWriteSync(&dstPB);
  1692.     }
  1693.  
  1694.     /* make sure there were no errors at the destination */
  1695.     if ( dstError != noErr )
  1696.         return ( dstError );
  1697.  
  1698.     /* make sure the only error at the source was eofErr */
  1699.     if ( srcError != eofErr )
  1700.         return ( srcError );
  1701.  
  1702.     return ( noErr );
  1703. }
  1704.  
  1705. /*****************************************************************************/
  1706.  
  1707. pascal    OSErr    GetFileLocation(short refNum,
  1708.                                 short *vRefNum,
  1709.                                 long *dirID,
  1710.                                 StringPtr fileName)
  1711. {
  1712.     FCBPBRec pb;
  1713.     OSErr error;
  1714.  
  1715.     pb.ioNamePtr = fileName;
  1716.     pb.ioVRefNum = 0;
  1717.     pb.ioRefNum = refNum;
  1718.     pb.ioFCBIndx = 0;
  1719.     error = PBGetFCBInfoSync(&pb);
  1720.     *vRefNum = pb.ioFCBVRefNum;
  1721.     *dirID = pb.ioFCBParID;
  1722.     return ( error );
  1723. }
  1724.  
  1725. /*****************************************************************************/
  1726.  
  1727. pascal    OSErr    FSpGetFileLocation(short refNum,
  1728.                                    FSSpec *spec)
  1729. {
  1730.     return ( GetFileLocation(refNum, &(spec->vRefNum), &(spec->parID), spec->name) );
  1731. }
  1732.  
  1733. /*****************************************************************************/
  1734.  
  1735. pascal    OSErr    CopyDirectoryAccess(short srcVRefNum,
  1736.                                     long srcDirID,
  1737.                                     StringPtr srcName,
  1738.                                     short dstVRefNum,
  1739.                                     long dstDirID,
  1740.                                     StringPtr dstName)
  1741. {    
  1742.     OSErr err;
  1743.     GetVolParmsInfoBuffer infoBuffer;    /* Where PBGetVolParms dumps its info */
  1744.     long    dstServerAdr;                /* AppleTalk server address of destination (if any) */
  1745.     Boolean    dstHasBlankAccessPrivileges;
  1746.     long    ownerID, groupID, accessRights;
  1747.     long    tempLong;
  1748.  
  1749.     /* See if destination supports directory access control */
  1750.     tempLong = sizeof(infoBuffer);
  1751.     err = HGetVolParms(dstName, dstVRefNum, &infoBuffer, &tempLong);
  1752.     if ( (err != noErr) && (err != paramErr) )
  1753.         return ( err );
  1754.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  1755.     {
  1756.         dstServerAdr = infoBuffer.vMServerAdr;
  1757.         dstHasBlankAccessPrivileges = hasBlankAccessPrivileges(infoBuffer);
  1758.     }
  1759.     else
  1760.         /* If destination doesn't support access privileges, */
  1761.         /* then there's nothing left to do here */
  1762.         return ( noErr );
  1763.  
  1764.     /* We may have to do something with access privileges at the destination. */
  1765.     
  1766.     accessRights = 0;    /* clear so we can tell if anything was done with this */
  1767.  
  1768.     /* See if source supports directory access control and is on same server */
  1769.     tempLong = sizeof(infoBuffer);
  1770.     err = HGetVolParms(srcName, srcVRefNum, &infoBuffer, &tempLong);
  1771.     if ( (err != noErr) && (err != paramErr) )
  1772.         return (err);
  1773.     if ( (err != paramErr) && hasAccessCntl(infoBuffer) )
  1774.     {
  1775.         /* Make sure both locations are on the same file server */
  1776.         if ( dstServerAdr == infoBuffer.vMServerAdr )
  1777.         {
  1778.             /* copy'm */
  1779.             err = HGetDirAccess(srcVRefNum, srcDirID, srcName, &ownerID, &groupID, &accessRights);
  1780.             if ( err == noErr )
  1781.                 err = HSetDirAccess(dstVRefNum, dstDirID, dstName, ownerID, groupID, accessRights);
  1782.         }
  1783.     }
  1784.     return ( err );
  1785. }
  1786.  
  1787. /*****************************************************************************/
  1788.  
  1789. pascal    OSErr    FSpCopyDirectoryAccess(const FSSpec *srcSpec,
  1790.                                        const FSSpec *dstSpec)
  1791. {
  1792.     return ( CopyDirectoryAccess(srcSpec->vRefNum, srcSpec->parID, (StringPtr)srcSpec->name,
  1793.                                 dstSpec->vRefNum, dstSpec->parID, (StringPtr)dstSpec->name) );
  1794. }
  1795.  
  1796. /*****************************************************************************/
  1797.  
  1798. pascal    OSErr    HMoveRenameCompat(short vRefNum,
  1799.                                   long srcDirID,
  1800.                                   ConstStr255Param srcName,
  1801.                                   long dstDirID,
  1802.                                   StringPtr dstpathName,
  1803.                                   StringPtr copyName)
  1804. {
  1805.     OSErr                    error;
  1806.     GetVolParmsInfoBuffer    volParmsInfo;
  1807.     long                    infoSize = sizeof(GetVolParmsInfoBuffer);
  1808.     short                    realVRefNum;
  1809.     long                    realParID;
  1810.     Str255                    realName;
  1811.     short                    tempItemsVRefNum;
  1812.     long                    tempItemsDirID;
  1813.     Boolean                    isDirectory;
  1814.     
  1815.     /* get volume attributes */
  1816.     error = HGetVolParms((StringPtr)srcName, vRefNum, &volParmsInfo, &infoSize);
  1817.     if ( error == noErr )
  1818.     {
  1819.         /* if volume supports move and rename, use it and return */
  1820.         if ( hasMoveRename(volParmsInfo) )
  1821.             return (HMoveRename(vRefNum, srcDirID, srcName, dstDirID, dstpathName, copyName));
  1822.     }
  1823.     else if ( error != paramErr )    /* paramErr is OK, it just means this volume doesn't support GetVolParms */
  1824.         return ( error );
  1825.     
  1826.     /* MoveRename isn't supported by this volume, so do it by hand */
  1827.     
  1828.     /* if copyName isn't supplied, we can simply CatMove and return */
  1829.     if ( copyName == NULL )
  1830.         return ( CatMove(vRefNum, srcDirID, srcName, dstDirID, dstpathName) );
  1831.     
  1832.     /* renaming is required, so we have some work to do... */
  1833.     
  1834.     /* get the object's real name */
  1835.     error = GetObjectLocation(vRefNum, srcDirID, (StringPtr)srcName,
  1836.                                 &realVRefNum, &realParID, realName, &isDirectory);
  1837.     if ( error != noErr )
  1838.         return ( error );
  1839.     
  1840.     /* find temporary items folder */
  1841.     error = FindFolder(realVRefNum, kTemporaryFolderType, kCreateFolder,
  1842.                         &tempItemsVRefNum, &tempItemsDirID);
  1843.     if ( error != noErr )
  1844.         return ( error );
  1845.     
  1846.     /* move the object to a temporary items folder for renaming */
  1847.     error = CatMove(realVRefNum, realParID, realName, tempItemsDirID, NULL);
  1848.     if ( error != noErr )
  1849.         return ( error );
  1850.     
  1851.     /* rename the object */    
  1852.     error = HRename(tempItemsVRefNum, tempItemsDirID, realName, copyName);
  1853.     if ( error == noErr )
  1854.     {
  1855.         /* move object to its new home */
  1856.         error = CatMove(tempItemsVRefNum, tempItemsDirID, copyName, dstDirID, dstpathName);
  1857.         if ( error == noErr )
  1858.             return ( error );    /* IF NO ERRORS EXIT HERE! */
  1859.         
  1860.         /* Error handling: rename object back to original name - keep real error */
  1861.         (void) HRename(tempItemsVRefNum, tempItemsDirID, copyName, realName);
  1862.     }
  1863.     
  1864.     /* Error handling: move object back to original location - keep real error */
  1865.     (void) CatMove(tempItemsVRefNum, tempItemsDirID, realName, realParID, NULL);
  1866.     
  1867.     return ( error );
  1868. }
  1869.  
  1870. /*****************************************************************************/
  1871.  
  1872. pascal    OSErr    FSpMoveRenameCompat(const FSSpec *srcSpec,
  1873.                                     const FSSpec *dstSpec,
  1874.                                     StringPtr copyName)
  1875. {
  1876.     /* make sure the FSSpecs refer to the same volume */
  1877.     if (srcSpec->vRefNum != dstSpec->vRefNum)
  1878.         return (diffVolErr);
  1879.     return ( HMoveRenameCompat(srcSpec->vRefNum, srcSpec->parID, srcSpec->name,
  1880.                       dstSpec->parID, (StringPtr)dstSpec->name, copyName) );
  1881. }
  1882.  
  1883. /*****************************************************************************/
  1884.  
  1885. pascal    void    BuildAFPVolMountInfo(short theFlags,
  1886.                                      char theNBPInterval,
  1887.                                      char theNBPCount,
  1888.                                      short theUAMType,
  1889.                                      Str31 theZoneName,
  1890.                                      Str31 theServerName,
  1891.                                      Str27 theVolName,
  1892.                                      Str31 theUserName,
  1893.                                      Str8 theUserPassWord,
  1894.                                      Str8 theVolPassWord,
  1895.                                      MyAFPVolMountInfoPtr theAFPInfo)
  1896. {
  1897.     /* Fill in an AFPVolMountInfo record that can be passed to VolumeMount */
  1898.     theAFPInfo->length = sizeof(MyAFPVolMountInfo);
  1899.     theAFPInfo->media = AppleShareMediaType;
  1900.     theAFPInfo->flags = theFlags;
  1901.     theAFPInfo->nbpInterval = theNBPInterval;
  1902.     theAFPInfo->nbpCount = theNBPCount;
  1903.     theAFPInfo->uamType = theUAMType;
  1904.     theAFPInfo->zoneNameOffset = (short)((long)theAFPInfo->zoneName - (long)theAFPInfo);
  1905.     theAFPInfo->serverNameOffset = (short)((long)theAFPInfo->serverName - (long)theAFPInfo);
  1906.     theAFPInfo->volNameOffset = (short)((long)theAFPInfo->volName - (long)theAFPInfo);
  1907.     theAFPInfo->userNameOffset = (short)((long)theAFPInfo->userName - (long)theAFPInfo);
  1908.     theAFPInfo->userPasswordOffset = (short)((long)theAFPInfo->userPassword - (long)theAFPInfo);
  1909.     theAFPInfo->volPasswordOffset = (short)((long)theAFPInfo->volPassword - (long)theAFPInfo);
  1910.     
  1911.     BlockMoveData(theZoneName, theAFPInfo->zoneName, theZoneName[0] + 1);
  1912.     BlockMoveData(theServerName, theAFPInfo->serverName, theServerName[0] + 1);
  1913.     BlockMoveData(theVolName, theAFPInfo->volName, theVolName[0] + 1);
  1914.     BlockMoveData(theUserName, theAFPInfo->userName, theUserName[0] + 1);
  1915.     BlockMoveData(theUserPassWord, theAFPInfo->userPassword, theUserPassWord[0] + 1);
  1916.     BlockMoveData(theVolPassWord, theAFPInfo->volPassword, theVolPassWord[0] + 1);
  1917. }
  1918.  
  1919. /*****************************************************************************/
  1920.  
  1921. pascal    OSErr    RetrieveAFPVolMountInfo(AFPVolMountInfoPtr theAFPInfo,
  1922.                                         short *theFlags,
  1923.                                         short *theUAMType,
  1924.                                         StringPtr theZoneName,
  1925.                                         StringPtr theServerName,
  1926.                                         StringPtr theVolName,
  1927.                                         StringPtr theUserName)
  1928. {
  1929.     OSErr        error;
  1930.     StringPtr    tempPtr;
  1931.         
  1932.     /* Retrieve the AFP mounting information from an AFPVolMountInfo record. */
  1933.     if ( theAFPInfo->media == AppleShareMediaType )
  1934.     {
  1935.         *theFlags = theAFPInfo->flags;
  1936.         *theUAMType = theAFPInfo->uamType;
  1937.         
  1938.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->zoneNameOffset);
  1939.         BlockMoveData(tempPtr, theZoneName, tempPtr[0] + 1);
  1940.         
  1941.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->serverNameOffset);
  1942.         BlockMoveData(tempPtr, theServerName, tempPtr[0] + 1);
  1943.         
  1944.         tempPtr = (StringPtr)(StringPtr)((long)theAFPInfo + theAFPInfo->volNameOffset);
  1945.         BlockMoveData(tempPtr, theVolName, tempPtr[0] + 1);
  1946.         
  1947.         tempPtr = (StringPtr)((long)theAFPInfo + theAFPInfo->userNameOffset);
  1948.         BlockMoveData(tempPtr, theUserName, tempPtr[0] + 1);
  1949.         
  1950.         error = noErr;
  1951.     }
  1952.     else
  1953.     {
  1954.         error = paramErr;
  1955.     }
  1956.     
  1957.     return ( error );
  1958. }
  1959.  
  1960. /*****************************************************************************/
  1961.  
  1962. pascal    OSErr    GetUGEntries(short objType,
  1963.                              UGEntryPtr entries,
  1964.                              long reqEntryCount,
  1965.                              long *actEntryCount,
  1966.                              long *objID)
  1967. {
  1968.     HParamBlockRec pb;
  1969.     OSErr error = noErr;
  1970.     UGEntry *endEntryArray = entries + reqEntryCount;
  1971.  
  1972.     pb.objParam.ioObjType = objType;
  1973.     *actEntryCount = 0;
  1974.     for ( ; (entries < endEntryArray) && (error == noErr); ++entries )
  1975.     {
  1976.         pb.objParam.ioObjNamePtr = (StringPtr)entries->name;
  1977.         pb.objParam.ioObjID = *objID;
  1978.         /* Files.h in the universal interfaces, PBGetUGEntrySync takes a CMovePBPtr */
  1979.         /* as the parameter. Inside Macintosh and the original glue used HParmBlkPtr. */
  1980.         /* A CMovePBPtr works OK, but this will be changed in the future  back to */
  1981.         /* HParmBlkPtr, so I'm just casting it here. */
  1982.         error = PBGetUGEntrySync((CMovePBPtr)&pb);
  1983.         if ( error == noErr )
  1984.         {
  1985.             entries->objID = *objID = pb.objParam.ioObjID;
  1986.             entries->objType = objType;
  1987.             ++*actEntryCount;
  1988.         }
  1989.     }
  1990.     return ( error );
  1991. }
  1992.